<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/3
 * Time: 13:43
 */
namespace app\api\controller;
use think\Db;
class Source extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    public function index(){
        //车长
        $where['is_status'] = 1;
        $where['is_del'] = 1;
        $res = Db::table('up_car_cclb')->field('cclb_id,cclb_name')->where($where)->select();
        //车型
        $where1['is_status'] = 1;
        $where1['is_del'] = 1;
        $res1 = Db::table('up_car_cx')->field('cx_id,cx_name')->where($where1)->select();
        //货物类型
        $where2['is_status'] = 1;
        $where2['is_del'] = 1;
        $res2 = Db::table('up_car_hwlx')->field('hwlx_id,hwlx_name')->where($where2)->select();
        //装修类型
        $where3['is_status'] = 1;
        $where3['is_del'] = 1;
        $res3 = Db::table('up_car_zxlx')->field('zxlx_id,zxlx_name')->where($where3)->select();
        $list['cc'] = $res;
        $list['cx'] = $res1;
        $list['hwlx'] = $res2;
        $list['zx'] = $res3;
        return json(['status' => 200, 'msg' => $list]);
    }
    //发布货源
    public function source(){
        extract(input());

        $list = Db::table('up_u_user')->where('user_id',$user_id)->find();
        //查询企业和加盟商是否签订合同
        $where['q_user_id'] = $list['user_id'];
        $where['j_user_id'] = $list['f_user_id'];
        $info = Db::table('up_contract')->where($where)->find();
        if($info['contract_id']==''){
            return json(['status'=>0,'msg'=>"没有和加盟商签订电子合同,请先签订电子合同,再发布货源"]);
        }else{
            if($info['contract_status']==1||$info['contract_status']==2||$info['contract_status']==3){
                if($info['contract_status']==''){
                    return json(['status'=>0,'msg'=>"合同结算周期不能为空"]);
                }
                if($info['contract_date']==''){
                    return json(['status'=>0,'msg'=>"合同每月结算日期不能为空"]);
                }
            }

        }
        //出发地
        $order_type = input('param.order_type');//1询价单  2直接发布
        if($order_type==1){
            $data['order_true'] = 3;
        }else{
            $data['order_true'] = 1;
        }
        if($info['contract_status']==1){
            $data['contract_status'] = 1;
        }
        if($info['contract_status']==2){
            $data['contract_status'] = 2;
        }
        if($info['contract_status']==3){
            $data['contract_status'] = 3;
        }
        $order_go_sheng = input('param.order_go_sheng');//出发省
        $order_go_shi = input('param.order_go_shi');
        $order_go_qu = input('param.order_go_qu');
        $order_go_address = input('param.order_go_address');
        $order_go_lon = input('param.order_go_lon');
        $order_go_lat = input('param.order_go_lat');
        //目的地
        $order_to_sheng = input('param.order_to_sheng');
        $order_to_shi = input('param.order_to_shi');
        $order_to_qu = input('param.order_to_qu');
        $order_to_address = input('param.order_to_address');
        $order_to_lon = input('param.order_to_lon');
        $order_to_lat = input('param.order_to_lat');
        //货物
        $order_goods_name = input('param.order_goods_name');
        $order_goods_ton = input('param.order_goods_ton');
        $order_goods_m = input('param.order_goods_m');
        $order_freight = input('param.order_freight');
        $order_zf_type = input('param.order_zf_type');
        $order_zc_time = input('param.order_zc_time');
        $zxlx_id = input('param.zxlx_id');
        $cx_id = input('param.cx_id');
        $order_cc = input('param.order_cc');
        $hwlx_id = input('param.hwlx_id');
        $order_intro = input('param.order_intro');
        $order_goods_img = input('param.order_goods_img');
        $order_number = date('YmdHi') . str_pad(mt_rand(1, 9999), 6, '0', STR_PAD_LEFT).$user_id;

        if($info['contract_status']==1||$info['contract_status']==2||$info['contract_status']==3){
            $contract_date = date('Y-m-'.$info['contract_date'],strtotime($info['contract_status'].'month'));
            $data['order_contract_date'] =$contract_date;
            $data['con_type'] =1;
        }elseif ($info['contract_status']==4){
            $data['con_type'] =2;
            $data['jiesuan'] =2;
        }elseif ($info['contract_status']==5){
            $data['con_type'] =3;
            $data['jiesuan'] =2;
        }


        $data['order_num'] = $order_number;
        $data['order_type'] = $order_type;
        $data['order_go_sheng'] = $order_go_sheng;
        $data['order_go_shi'] = $order_go_shi;
        $data['order_go_qu'] = $order_go_qu;
        $data['order_go_address'] = $order_go_address;
        $data['order_go_lon'] = $order_go_lon;
        $data['order_go_lat'] = $order_go_lat;
        $data['order_to_sheng'] = $order_to_sheng;
        $data['order_to_shi'] = $order_to_shi;
        $data['order_to_qu'] = $order_to_qu;
        $data['order_to_address'] = $order_to_address;
        $data['order_to_lon'] = $order_to_lon;
        $data['order_to_lat'] = $order_to_lat;
        $data['order_goods_name'] = $order_goods_name;
        $data['order_goods_ton'] = $order_goods_ton;
        $data['order_goods_m'] = $order_goods_m;
        $data['order_freight'] = $order_freight;
        $data['order_zf_type'] = $order_zf_type;
        $data['order_zc_time'] = $order_zc_time;
        $data['zxlx_id'] = $zxlx_id;
        $data['cx_id'] = $cx_id;
        $data['order_cc'] = $order_cc;
        $data['hwlx_id'] = $hwlx_id;
        $data['order_intro'] = $order_intro;
        $data['order_goods_img'] = $order_goods_img;
        $data['order_fb_time'] =date('Y-m-d H:i:s');
        $data['order_fb_ymr'] =date('Y-m-d');
        $data['order_fb_ym'] =date('Y-m');
        $data['order_qy_user'] = $user_id;
        $data['order_jms_user'] = $list['f_user_id'];

        $district = Db::table('up_area')->where('district_id',$order_to_sheng)->value('district');
        $data['order_quyu'] = myArea($district);
        $res = Db::table('up_order')->insertGetId($data);
        //插入消息表
        $dataList['n_title'] = $list['user_gongsi'].'已向您发送一份询价单，请尽快处理';
        $dataList['n_type'] =  2;
        $dataList['n_notice_type'] = 2;
        $dataList['n_user_id'] = ','.$list['f_user_id'].',';
        $dataList['n_read'] = ',0,';
        $dataList['n_delete'] = ',0,';
        $dataList['n_time'] = date('Y-m-d H:i:s');
        $dataList['n_status'] = 2;
        Db::table('up_notice')->insertGetId($dataList);
        if($res){
            //推送
            $list1 = [];
            $v = (string)$list['f_user_id'];
//            array_push($list1, $v);
            $code=2;
            $str = array('msg'=>$list['user_gongsi'].'已向您发送一份订单，请尽快处理','type'=>10,'order_id'=>$res);
            $str1 = $list['user_gongsi'].'已向您发送一份订单，请尽快处理';
            try{
                $this->Jpush1(1, $v, $str1, $str,$code);
            }catch(\Exception $e){

            }
            return json(['status'=>200,'msg'=>"发布成功"]);
        }else{
            return json(['status'=>0,'msg'=>'发布失败']);
        }
    }
}